package com.think.archive.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.think.archive.common.ResultBean;
import com.think.archive.service.InfoArchiveService;
import com.think.archive.service.InfoFileService;
import com.think.archive.service.OcrService;
import com.think.archive.util.CommonUtil;
import com.think.archive.util.baidu.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.URLEncoder;


@RestController
@RequestMapping("ocr")
public class OcrController {

    @Autowired
    OcrService ocrService;

    @Autowired
    InfoFileService fileService;

    @Autowired
    InfoArchiveService archiveService;


    @Value("${uploadFile.location}")
    private String uploadFileLocation;
    @Value("${uploadFile.resourceHandler}")
    private String resourceHandler;

    @RequestMapping("updatePic")
    public ResultBean updatePic(HttpServletRequest request, @RequestParam("file") MultipartFile file){
        if (file == null || file.isEmpty()) {
            return ResultBean.createResultByError("上传文件为空...");
        }
        try {

            String uuid= CommonUtil.getSimpleUUID();
            String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
            String fileName = file.getOriginalFilename();
            String[] f=fileName.split("\\.");
            String file_end="jpg";
            if(!"blob".equals(f[f.length-1])){
                file_end=f[f.length-1];
            }
            String fileServerPath = basePath + resourceHandler.substring(0, resourceHandler.lastIndexOf("/") + 1) + uuid+"."+file_end;
            File saveFile = new File(uploadFileLocation, uuid +"."+file_end);
            if(!saveFile.exists()){
                saveFile.mkdirs();
            }
            file.transferTo(saveFile);

            JSONObject jsonpObject=JSON.parseObject(ocrService.recognise(URLEncoder.encode(Base64Util.encode(file.getBytes()),"utf-8"),"4c2f6be3000ea8e81811df1853010cc6",0));
            if("0".equals(jsonpObject.getString("error_code"))){
                JSONObject res=archiveService.insert(jsonpObject);
                fileService.insertUpdateFile(request,file,res.getString("_id"));
                return ResultBean.createResultBySuccess(res);
            }
            else{
                return ResultBean.createResultByError(jsonpObject.getString("error_msg"));
            }
        }catch (Exception e) {
            e.printStackTrace();
            return ResultBean.createResultByError(e.getMessage());
        }
    }

    @RequestMapping("findArchive")
    public ResultBean findArchive(){
        return ResultBean.createResultByError();
    }
}
